package com.learn.xueer.data.bindadapter

import android.graphics.Bitmap
import android.graphics.drawable.Drawable
import android.os.SystemClock
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.transition.Transition
import android.view.View
import android.widget.CheckBox
import android.widget.CompoundButton
import android.widget.EditText
import android.widget.ImageView
import androidx.databinding.BindingAdapter
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.SizeUtils
import com.bumptech.glide.Glide
import com.bumptech.glide.Priority
import com.bumptech.glide.load.DecodeFormat
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.bumptech.glide.load.resource.bitmap.CircleCrop
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions
import com.bumptech.glide.request.RequestOptions
import com.bumptech.glide.request.target.CustomTarget
import com.bumptech.glide.request.target.SimpleTarget
import com.learn.xueer.R
import com.learn.xueer.app.util.CornerTransform
import com.learn.xueer.app.weight.preference.MyColorCircleView
import me.hgj.jetpackmvvm.base.appContext
import me.hgj.jetpackmvvm.ext.view.textString

/**
 * 作者　: hegaojian
 * 时间　: 2019/12/23
 * 描述　: 自定义 BindingAdapter
 */
object CustomBindAdapter {

    @BindingAdapter(value = ["checkChange"])
    @JvmStatic
    fun checkChange(checkbox: CheckBox, listener: CompoundButton.OnCheckedChangeListener) {
        checkbox.setOnCheckedChangeListener(listener)
    }

    @BindingAdapter(value = ["showPwd"])
    @JvmStatic
    fun showPwd(view: EditText, boolean: Boolean) {
        if (boolean) {
            view.inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        } else {
            view.inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        }
        view.setSelection(view.textString().length)
    }

    @BindingAdapter(value = ["imageUrl"])
    @JvmStatic
    fun imageUrl(view: ImageView, url: String?) {
        if (url.isNullOrEmpty()) return
        Glide.with(view.context.applicationContext)
            .load(url)
            .transition(DrawableTransitionOptions.withCrossFade(500))
            .placeholder(R.mipmap.default_book)
            .error(R.mipmap.default_book)
            .into(view)
    }

    @BindingAdapter(value = ["drawableUrl"])
    @JvmStatic
    fun drawableUrl(view: ImageView, drawable: Int) {
        Glide.with(view.context.applicationContext)
            .load(drawable)
            .into(view)
    }

    @BindingAdapter(value = ["circleImageUrl"])
    @JvmStatic
    fun circleImageUrl(view: ImageView, url: String) {
        if (url.isNullOrEmpty()) return
        if (url.startsWith("data:image/svg+xml;base64,")) {
            url.removePrefix("data:image/svg+xml;base64,")
        }
        Glide.with(view.context.applicationContext)
            .asBitmap()
            .load(url)
            .placeholder(R.mipmap.default_avatar)
            .error(R.mipmap.default_avatar)
            .centerCrop()
            .apply(RequestOptions.bitmapTransform(CircleCrop()))
            .into(object : CustomTarget<Bitmap>() {
                override fun onResourceReady(
                    p0: Bitmap,
                    p1: com.bumptech.glide.request.transition.Transition<in Bitmap>?
                ) {
                    view.setImageBitmap(p0)
                }

                override fun onLoadCleared(p0: Drawable?) {

                }

            })
    }

    @BindingAdapter(value = ["cornerImageUrl"])
    @JvmStatic
    fun cornerImageUrl(view: ImageView, url: String) {
        LogUtils.e("url:${url}")

        if (url.isNullOrEmpty()) return
        try {
            if (url.startsWith("data:image/svg+xml;base64,")) {
                url.removePrefix("data:image/svg+xml;base64,")
            }
            Glide.with(view.context.applicationContext)
                .load(url)
                .centerCrop()
                .apply(
                    RequestOptions.bitmapTransform(
                        CornerTransform(
                            view.context.applicationContext,
                            SizeUtils.dp2px(15f).toFloat()
                        )
                    )
                )
                .transition(DrawableTransitionOptions.withCrossFade(500))
                .into(view)
        } catch (e: Exception) {
            LogUtils.e(e.message)
        }

    }

    @BindingAdapter(value = ["colorCircleViewColor"])
    @JvmStatic
    fun colorCircleViewColor(view: MyColorCircleView, color: Int) {
        view.setView(color)
    }

    @BindingAdapter(value = ["afterTextChanged"])
    @JvmStatic
    fun EditText.afterTextChanged(action: (String) -> Unit) {
        addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(s: Editable?) {

            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
                action(s.toString())
            }
        })
    }

    @BindingAdapter(value = ["noRepeatClick"])
    @JvmStatic
    fun setOnClick(view: View, clickListener: () -> Unit) {
        val mHits = LongArray(2)
        view.setOnClickListener {
            System.arraycopy(mHits, 1, mHits, 0, mHits.size - 1)
            mHits[mHits.size - 1] = SystemClock.uptimeMillis()
            if (mHits[0] < SystemClock.uptimeMillis() - 500) {
                clickListener.invoke()
            }
        }
    }


}